﻿function search(page) {
    if (page == 1) {
        usergrid.setOptions(
             {
                 parms:
                [
                    { name: "userName", value: $("#txtUserCode").val() },
                    { name: "realName", value: $("#txtRealName").val() },
                    { name: "page", value: 1 }
                ]
             });
    }
    else {
        usergrid.setOptions(
             {
                 parms:
                [
                    { name: "userName", value: $("#txtUserCode").val() },
                    { name: "realName", value: $("#txtRealName").val() }
                ]
             });
    }
    usergrid.loadData();
}


function grid_user_add() {
    //location.href =decodeURIComponent("/User/UserAdd") ;
    grid_user_show();
}

function save_User() {
    //addDialog.frame.save();
    var form = $('#frmUser');
    form.submit();
}
 
function grid_user_edit() {
    if (usergrid.getSelecteds().length == 0) {
        $.ligerDialog.warn('至少选择一个用户进行修改！', '温馨提示');
        return;
    }
    var user = usergrid.getSelected();
    grid_user_show(user.UserName,user.RealName,user.TelePhone,user.HandPhone); 
}

function grid_user_show(username,realName,telephone,handphone) {
    var title = "添加用户";
    if (username != null) {
        title = "修改用户【 " + username + " 】"
        user_set(username, realName, telephone, handphone);
    } else {
        user_reset();
    }
    saveDialog = $.ligerDialog.open({
        title: title,
        id: 'userDialog',
        name: 'userDialog',
        slide: false,
        isHidden: false,
        modal: true,
        target: $("#frmUser"),
        //        url: "/User/UserAdd",
        width: 330, isResize: true, allowClose: false,
        buttons: [
                    { text: '保存', onclick: save_User },
                    {
                        text: '取消', onclick: function (item, dialog) {
                            dialog.hide();
                            user_reset();
                        }
                    }
                ]
    });
}
function user_set(username, realName, telephone, handphone) {
    //$("#txtUserName").ligerTextBox('option', 'disabled', false);
    $("#txtUserName").val(username);
    txtuserName.setDisabled();
    $("#txtRealUserName").val(realName);
    $("#txtTelephone").val(telephone);
    $("#txtHandphone").val(handphone);
    $("#isEdit").val("1");
    $("#lblmsg").html("");
}


function user_reset() {
    $(".l-text-field").ligerHideTip();
    //$("#txtUserName").ligerTextBox('option', 'disabled', true);
    txtuserName.setEnabled();
    $("#txtUserName").val("");

    $("#txtRealUserName").val("");
    $("#txtTelephone").val("");
    $("#txtHandphone").val("");
    $("#isEdit").val("0");
    $("#lblmsg").html("");
}

function grid_user_delete() {
    var userNames = "";
    var names = "";

    if (usergrid.getSelecteds().length == 0) {
        $.ligerDialog.warn('至少选择一个用户进行删除！', '温馨提示');
        return;
    }
    var selected = usergrid.getSelecteds();
    if (selected.length == 0) {
        $.ligerDialog.warn('请选择要删除的数据！', '温馨提示');
        return;
    }

    for (var i = 0; i < selected.length; i++) {
        userNames += selected[i].UserName + ",";
        names += selected[i].RealName + ",";
    }
    names = names.substring(0, names.length - 1);
    userNames = userNames.substring(0, userNames.length - 1);
    user_delete(userNames, names);

}

function user_delete(userName, realName) {
    $.ligerDialog.confirm('确定删除用户[' + realName + ']吗？', '温馨提示', function (result) {
        if (result) {
            $.ajax({
                url: '/User/UserDelete',
                data: "userNames=" + userName,
                type: "post",
                success: function (result) {
                    if (result == "1") {
                        //employee.deleteSelectedRow();
                        $.ligerDialog.success("成功删除用户[" + realName + "]。");
                        grid_user_refresh();
                    } else {
                        $.ligerDialog.error('删除失败！', '温馨提示');
                    }
                }
            });
        }
    });
}
function changestate(userName, state,realName) {
    var statestr = "禁用";
    if (state == 1) {
        statestr = "启用";
    }
    $.ligerDialog.confirm('确定【' + statestr + '】用户['+realName+']吗？', '温馨提示', function (result) {
        if (result) {
            $.ajax({
                url: '/User/UserChangeStatus',
                data: "userName=" + userName + "&status=" + state,
                type: "post",
                success: function (result) {
                    if (result == "1") {
                        //employee.deleteSelectedRow();
                        $.ligerDialog.success(statestr + "用户成功");
                        grid_user_refresh();
                    } else {
                        $.ligerDialog.error(statestr + "用户失败！", '温馨提示');
                    }
                }
            });
        }
    });
}
function grid_user_refresh() {
    usergrid.setOptions(
             {
                 parms:
                [
                    { name: "page", value: 1 }
                ]
             });
    usergrid.loadData();
}
 